<template>
  <div>
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn='false'
      :onColumnsChange="columns => this.columns = columns"
    >
    <template slot="controls"  v-if="dataType !== '10461001'">
        <el-button size="small" type="primary" @click="addHandle">新增</el-button>
        <el-button size="small" type="primary" @click="importHandle">整体导入</el-button>
      <el-button size="small" type="primary" @click="exportHandle">整体导出</el-button>
      <el-button size="small" type="primary" @click="downHandle">模板下载</el-button>
      </template>
    </FilterTable>
    <BaseDialog
      :visible.sync="UploadFileIsShow"
      title="整体导入"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <!-- <UploadFile
        :action-url="actionUrl"
        :initial-value="form.fileList"
        :autoUpload='false'
        @change="successHandler"
        @error="errorHandler"
        :fileTypes="['xls','xlsx']"
      >选择导入文件</UploadFile> -->

      <importFile
        title="导入"
        :actionUrl="actionUrl"
        @close="successHandler"
        />
    </BaseDialog>
    <BaseDialog
      :visible.sync="visible"
      :title="dialogTitle"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <modelCarsModal :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import {
  queryCompeteModels,
  wholeExport,
  importPackages,
} from '@/api/potenCus/basedata/competitiveModels/index';
import { dictionary } from '@/utils/dictMixin';
import { getDataType } from '@/assets/js/auth';
import {exportExcelFile} from '@/utils/index';
import modelCarsModal from './modelCarsModal';
export default {
  name: 'followTask',
  mixins: [dictionary],
  components: {
    modelCarsModal
  },
  data() {
    return {
      dataType: getDataType('dataType'),
      fetchParams: { level:2 },
      visible: false,
      delVisible: false,
      UploadFileIsShow: false,
      dialogTitle: '',
      fetchApi: queryCompeteModels,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      conf:{ add: '新增', edit: '编辑', import: '导入', export: '导出', down: '下载', },
      querInfo:{},
      form: {
        fileList: []
      },
      actionUrl:"/api/cyxdms.clue/basedata/failModels/importPackages"
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 60,
          fixed: 'left',
          render: (props, h) => {
            if(this.dataType!== '10461001'){
              return (
              <div>
                <el-button  size="mini" type="text" disabled={ this.dataType == 10461001&& props.row.dataSource==10451002? true:false }  onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
              </div>
            );
            }else{
              return (<div>-</div>);
            }
            
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '品牌',
          dataIndex: 'brandName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '车型车系',
          dataIndex: 'failSeries',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '数据来源',
          dataIndex: 'dataSource',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1045'),
          render: props => {
            return <span>{this.createDictText(props.row.dataSource, '1045')}</span>;
          }
        },
        {
          title: '是否有效',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isValid, '1004')}</span>;
          }
        }
      ]
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    // 整体导入
    importHandle() {
      this.handleType = 'import';
      this.UploadFileIsShow=true
      this.form.fileList = [];
    },
    successHandler(val) {
      if(val&&val.length>0){
        console.log(val,"导入")

        this.form.fileList = val;
        // val.forEach(x=>{
        //   if(x.url.errorList.length > 0){
        //     this.$message.warning(`${x.url.errorList[0].errorMsg}`);
        //   }else{
        //      this.form.fileList = val;
        //       this.UploadFileIsShow = false
        //       this.fetchParams = { ...this.fetchParams };
        //       this.$message.success('导入成功')
        //   }
        // })
      }
        this.UploadFileIsShow = false
        this.fetchParams = { ...this.fetchParams };
    },
    errorHandler(val){
      console.log(val,"删除")
    },
    // 整体导出
    exportHandle() {
      this.handleType = 'export';
      this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
	        confirmButtonText: '下载',
	        cancelButtonText: '取消',
	        type: 'warning'
	      }).then(() => {
            wholeExport(1).then( response => {
              console.log(response,"response")
              exportExcelFile(response.data,'竞品车型定义');
              this.exportLoading = false;
            }).catch( err => {
              console.log(err)
            })
	      }).catch(() => {
          console.log('456')
	        this.$message({
	          type: 'info',
	          message: '已取消'
	        });
	      });
    },
    // 模板下载
    downHandle() {
      this.handleType = 'down';
      this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
	        confirmButtonText: '下载',
	        cancelButtonText: '取消',
	        type: 'warning'
	      }).then(() => {
            wholeExport(2).then( response => {
              console.log(response,"response")
              exportExcelFile(response.data,'竞品车型定义');
              this.exportLoading = false;
            }).catch( err => {
              console.log(err)
            })
	      }).catch(() => {
          console.log('456')
	        this.$message({
	          type: 'info',
	          message: '已取消'
	        });
	      });
    },
    closeVisible(){
      this.delVisible = false;
    },
    showHandler() {
      this.dialogTitle = `竞品车型车系${this.conf[this.handleType]}`;
      this.visible = true;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({ sort: 'wayId', order: 'desc'}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  },
  
  created() {
  }
};
</script>

<style lang="less" scoped>
</style>
